package com.tusuo.stds.ui.reward;

import android.content.Intent;
import android.support.annotation.NonNull;
import android.text.TextUtils;
import android.view.View;
import android.widget.LinearLayout;
import android.widget.RadioButton;
import android.widget.TextView;

import com.andreabaccega.widget.FormEditText;
import com.tusuo.stds.R;
import com.tusuo.stds.ui.BaseToolbarActivity;
import com.tusuo.stds.ui.ToolbarOwner;
import com.tusuo.stds.ui.pay.MyPayActivity_;
import com.tusuo.stds.util.Utils;
import com.tusuo.stds.util.validator.RewardNumberValidator;

import org.androidannotations.annotations.AfterInject;
import org.androidannotations.annotations.AfterViews;
import org.androidannotations.annotations.Click;
import org.androidannotations.annotations.EActivity;
import org.androidannotations.annotations.ViewById;

import java.util.ArrayList;


@EActivity(R.layout.activity_reward)
public class RewardActivity extends BaseToolbarActivity<RewardView,RewardPresenter> implements RewardView {

    RewardPresenter mPresenter;
    private String sceneTitle;
    private int sceneId;

    @ViewById(R.id.reward_rb_money_1)
    RadioButton rbMoney1;
    @ViewById(R.id.reward_rb_money_5)
    RadioButton rbMoney5;
    @ViewById(R.id.reward_rb_money_10)
    RadioButton rbMoney10;
    @ViewById(R.id.reward_rb_money_20)
    RadioButton rbMoney20;
    @ViewById(R.id.reward_rb_money_50)
    RadioButton rbMoney50;
    @ViewById(R.id.reward_rb_money_100)
    RadioButton rbMoney100;
    @ViewById(R.id.reward_rb_money_other)
    RadioButton rbMoneyOther;

    @ViewById(R.id.reward_et_other_money)
    FormEditText etMoneyOther;

    @ViewById(R.id.reward_layout_other)
    LinearLayout layoutOther;

    @ViewById(R.id.reward_tv_scene_title)
    TextView tvSceneTitle;

    private ArrayList<RadioButton> radioButtons ;
    private int payMoney = 1;
    private boolean haveInputMoney = false;
    private int[] moneys = new int[] {1,5,10,20,50,100};


    @NonNull
    @Override
    public RewardPresenter createPresenter() {
        mPresenter = new RewardPresenter();
        return mPresenter;
    }

    @AfterInject
    public void init() {
        radioButtons = new ArrayList<>();
        Intent intent = getIntent();
        sceneTitle = intent.getStringExtra("sceneTitle");
        sceneId = intent.getIntExtra("sceneId",-1);
    }

    @AfterViews
    public void initData() {
        setupUI(this,getResources().getColor(R.color.white),getResources().getColor(R.color.black));
        ToolbarOwner.Config config = new ToolbarOwner.Config("打赏", true, R.drawable.ic_back_dark, null);
        mToolbarOwner.setConfig(config);

        if (!TextUtils.isEmpty(sceneTitle)) {
            tvSceneTitle.setText(sceneTitle);
        }

        rbMoney1.setChecked(true);

        radioButtons.add(rbMoney1);
        radioButtons.add(rbMoney5);
        radioButtons.add(rbMoney10);
        radioButtons.add(rbMoney20);
        radioButtons.add(rbMoney50);
        radioButtons.add(rbMoney100);
        radioButtons.add(rbMoneyOther);

        etMoneyOther.addValidator(new RewardNumberValidator("打赏金额范围为1-200"));

        layoutOther.setVisibility(View.GONE);
    }

    /**
     * 相应所有选择打赏金额的RadioButton功能
     * @param v
     */
    public void selectMoney(View v) {
        switch (v.getId()) {
            case R.id.reward_rb_money_1 :
                changeMoney(0);
                break;
            case R.id.reward_rb_money_5 :
                changeMoney(1);
                break;
            case R.id.reward_rb_money_10 :
                changeMoney(2);
                break;
            case R.id.reward_rb_money_20 :
                changeMoney(3);
                break;
            case R.id.reward_rb_money_50 :
                changeMoney(4);
                break;
            case R.id.reward_rb_money_100 :
                changeMoney(5);
                break;
            case R.id.reward_rb_money_other :
                changeMoney(6);
                break;
        }

    }

    public void changeMoney(int index) {

        if (index == radioButtons.size() - 1) {
            etMoneyOther.setText("");
            layoutOther.setVisibility(View.VISIBLE);
            haveInputMoney = true;
        }else {
            haveInputMoney = false;
            etMoneyOther.setText("1");
            layoutOther.setVisibility(View.GONE);
        }

        if (index < moneys.length) {
            payMoney = moneys[index];
        }

        for (int i = 0; i < radioButtons.size(); i ++) {
            if (i != index){
                radioButtons.get(i).setChecked(false);
            }
        }
    }

    private int getInputMoney() {
        String inputMoneyStr = etMoneyOther.getText().toString().trim();
        int inputMoney = 0;
        if (!TextUtils.isEmpty(inputMoneyStr)) {
            inputMoney = Integer.parseInt(inputMoneyStr);
        }else {
            return -1;
        }
        return inputMoney;
    }

    @Click(R.id.reward_tv_pay)
    public void doPayAction(View v) {
        if (Utils.UI.isFastDoubleClick()) {
            return;
        }

        // 强制隐藏软键盘
        Utils.UI.closeInputMethod(this);

        if (haveInputMoney) {
            if (!etMoneyOther.testValidity()) {
                etMoneyOther.setText("");
                return;
            }
            payMoney = getInputMoney();
        }

        Intent intent = new Intent(this, MyPayActivity_.class);
        intent.putExtra("sceneId",sceneId);
        intent.putExtra("payType","reward");
        intent.putExtra("sceneTitle",sceneTitle);
        intent.putExtra("rewardMoney",payMoney);
        startActivity(intent);
    }

}
